package com.game.frame.GameFrame.GameFrameMain;

import com.game.utils.StaticValue;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 游戏面板类，继承自JPanel
 * 用于绘制游戏元素，如背景、角色等
 */
public class GamePanel extends JPanel {
    // 背景图片
    private BufferedImage backgroundImage;
    // 面板宽度
    private final int panelWidth;
    // 面板高度
    private final int panelHeight;
    // 当前背景索引
    private int currentBgIndex = 1;
    // 添加角色列表
    private List<Hero> characters = new ArrayList<>();
    // 添加野兽列表
    private List<Beast> beasts = new ArrayList<>();
    // 分数
    private GameCore gameCore;
    // 游戏窗口
    private GameWindow gameWindow;

    /**
     * 构造函数，初始化游戏面板的尺寸和背景颜色
     * @param width  面板宽度
     * @param height 面板高度
     */
    public GamePanel(int width, int height, GameWindow gameWindow) {
        this.panelWidth = width;
        this.panelHeight = height;
        this.gameWindow = gameWindow;
        
        setPreferredSize(new Dimension(width, height));
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        this.backgroundImage = StaticValue.bg01;
        this.gameCore = new GameCore(this, 0);

        // 创建一个计时器来更新角色的动画
        Timer animationTimer = new Timer(50, e -> {
            for (Hero character : characters) {
                character.updatePhysics();
                character.updateAnimation();
            }
            // 更新野兽的动画
            for (Beast beast : beasts) {
                beast.updateAnimation();
            }
            // 更新时间
            gameCore.updateTime();
            repaint();
        });
        animationTimer.start();
    }

    /**
     * 设置背景图片
     * @param bgIndex 背景索引
     */
    public void setBackgroundImage(int bgIndex) {
        switch (bgIndex) {
            case 1:
                this.backgroundImage = StaticValue.bg01;
                break;
            case 2:
                this.backgroundImage = StaticValue.bg02;
                break;
            case 3:
                this.backgroundImage = StaticValue.bg03;
                break;
            case 4:
                this.backgroundImage = StaticValue.bg04;
                break;
        }
        this.currentBgIndex = bgIndex;
        repaint();
    }

    /**
     * 获取游戏窗口
     * @return 游戏窗口
     */
    public GameWindow getGameWindow() {
        return gameWindow;
    }

    // 添加野兽的方法
    public void addBeast(Beast beast) {
        beasts.add(beast);
    }

    /**
     * 重写paintComponent方法，用于绘制游戏元素
     * @param g Graphics对象，用于绘图
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 移除死亡的野兽
        removeDeadBeasts();

        // 绘制背景
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, panelWidth, panelHeight, null);
        }

        // 绘画所有角色
        for (Hero character : characters) {
            character.drawBlood(g);
        }

        // 绘制所有野兽
        for (Beast beast : beasts) {
            if(!beast.isDead()){
                beast.drawBlood(g);
            }
        }

        // 绘制分数和时间
        gameCore.draw(g);
    }

    // 获取GameCore 实例
    public GameCore getGameCore() {
        return gameCore;
    }

    /**
     * 移除死亡的野兽
     */
    private void removeDeadBeasts() {
        beasts.removeIf(Beast::isDead);
    }

    /**
     * 更新游戏状态，移除死亡的野兽
     */
    public void updateGameState() {
        // 使用迭代器 安全删除已经死亡的野兽
        Iterator<Beast> iterator = beasts.iterator();
        while (iterator.hasNext()) {
            Beast beast = iterator.next();
            if (beast.isDead()) {
                iterator.remove();
            }
        }
    }

    // 获取画板尺寸的方法
    /**
     * 获取面板宽度
     * @return 面板宽度
     */
    public int getPanelWidth() {
        return panelWidth;
    }

    /**
     * 向面板中添加角色
     * @param character 要添加的角色
     */
    public void addCharacter(Hero character) {
        characters.add(character);
    }

    /**
     * 获取面板高度
     * @return 面板高度
     */
    public int getPanelHeight() {
        return panelHeight;
    }

    /**
     * 获取当前背景索引
     * @return 当前背景索引
     */
    public int getCurrentBgIndex() {
        return currentBgIndex;
    }

    /**
     * 清理所有野兽
     */
    public void clearBeasts() {
        beasts.clear();
        repaint();
    }

    /**
     * 清理所有角色
     */
    public void clearCharacters() {
        characters.clear();
        repaint();
    }

    public List<Hero> getCharacters() {
        return characters;
    }

    public List<Beast> getBeasts() {
        return beasts;
    }
}
